package org.jeecg.modules.API.until;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class YGCTPMsg extends DefaultConsumer {
    // 通道
    private Channel m_channel;
    // 交换机-队列列表映射
    private Map<String, List<String>> m_exchange_queue = new HashMap<>();
    // 队列-消息映射（routingkey）
    private Map<String, List<String>> m_queue_msg = new HashMap<>();

    private Map<String, Object> m_expire_time = new HashMap<String , Object>();

    public YGCTPMsg(Channel channel) throws IOException, TimeoutException {
        super(channel);
        //if ((m_exchange_queue.get(exchangeName) != null) || (m_exchange_queue.get(exchangeName).size() !=0)){
            //channel.close();
        //}else{
            m_channel = channel;
        //}

    }

    public void subscribeMsg(String exchangeName, String queueName, String routingKey) throws IOException {
        m_expire_time.put("x-message-ttl" , 10000);
        // 1. 判断是否已绑定交换机，如果没有则需要绑定交换机和创建队列
        if (m_exchange_queue.get(exchangeName) == null){
            if (exchangeName.equals("YGMD")){
                m_channel.exchangeDeclare(exchangeName, "fanout");
            }else {
                m_channel.exchangeDeclare(exchangeName, "topic");
            }
            ArrayList<String> lst_queue = new ArrayList<>();
            m_exchange_queue.put(exchangeName, lst_queue);
        }
        // 2. 判断已订阅的队列中是否有该队列，没有则需要订阅
        if (m_exchange_queue.get(exchangeName).contains(queueName) == false){

            m_channel.queueDeclare(queueName,true, false,false,null);
            if(exchangeName.equals("YGMD")){
                m_channel.queueBind(queueName,exchangeName,"",m_expire_time);
            }else {
                m_channel.queueBind(queueName,exchangeName,routingKey,m_expire_time);
            }
            List<String> lst_queue = m_exchange_queue.get(exchangeName);
            lst_queue.add(queueName);
            m_exchange_queue.put(exchangeName, lst_queue);
            ArrayList<String> lst_routingkey = new ArrayList<>();
            m_queue_msg.put(queueName, lst_routingkey);
        }
        // 3. 判断是否已订阅主题，没订阅则需要订阅
        if (m_queue_msg.get(queueName).contains(routingKey) == false){
            if(exchangeName.equals("YGMD")){
                m_channel.queueBind(queueName,exchangeName,"",m_expire_time);
            }else {
                m_channel.queueBind(queueName,exchangeName,routingKey,m_expire_time);
            }
            List<String> lst_routingkey = m_queue_msg.get(queueName);
            lst_routingkey.add(routingKey);
            m_queue_msg.put(queueName, lst_routingkey);
        }
        m_channel.basicConsume(queueName, false, this);
    }
}
